import { GameConfigData } from "../../common/GameConfig";

const { ccclass, property } = cc._decorator;
export let fishTypeView: pfSelecFish;
@ccclass
export default class pfSelecFish extends cc.Component {
  @property(cc.Prefab)
  pfFishItem: cc.Prefab = null;
  @property(cc.SpriteFrame)
  fishArr: cc.SpriteFrame[] = [];
  @property(cc.Node)
  nodeContent: cc.Node = null;
  _callback = null;

  setViewData(data) {
    if (data && data.callback) this._callback = data.callback;
    fishTypeView = this;
    this.createOrUpDateFishItem();
  }

  createOrUpDateFishItem() {
    for (let i = 0; i < GameConfigData.fishTimeConfig.length; i++) {
      let nodeFish = null;
      let fishItem = null;
      if (!this.nodeContent.children[i]) {
        nodeFish = cc.instantiate(this.pfFishItem);
        this.nodeContent.addChild(nodeFish);
      } else {
        nodeFish = this.nodeContent.children[i];
      }
      fishItem = nodeFish.getComponent(nodeFish.name);
      let fishData = GameConfigData.fishTimeConfig[i];
      let data = {
        fishConfig: fishData,
        spFish: this.fishArr[~~fishData.id - 1],
        callback: this._callback,
      };

      fishItem.setViewData(data);
    }
  }

  onButtonCloseCallback() {
    this.node.removeFromParent();
    this.node.destroy();
  }
}
